# -*- coding: utf-8 -*-
"""
Created on Thu Aug 08 18:55:38 2013

@author: Jeanhwea

A tool module for a better life
"""

import re
import sys

def hello():
    ''' Return a 'Hello World!' string '''
    return 'Hello World!'

def u2g(text):
    ''' Change UTF-8 encoding to GBK endcoding
    '''
    return text.decode('utf-8').encode('gbk')
    
def g2u(text):
    ''' Change GBK encoding to UTF-8 endcoding
    '''
    return text.decode('gbk').encode('utf-8')
    
def join2line(filepath):
    ''' Join a javascript file into a line
    '''
    ret = ''
    try:
        fin = open(filepath, 'r')
        remove_comment = re.compile(r'(.+)//.+')
        for _line in fin:
            _result = remove_comment.match(_line)
            if _result:
                _line = _result.group(1) # match comment, remove it
            _line = re.sub('^\s+', '', _line) # remove start spaces
            _line = re.sub('\s+$', '', _line) # remove end spaces
            ret += _line.translate(None, '\n').translate(None, '\r')
            # remove end of line
        fin.close()
        return ret
    except IOError:
        print >> sys.stderr, "Can not open or find %s" % filepath